package com.leonardo.beanme.metadatahandlers;

import org.xml.sax.Attributes;

import com.leonardo.beanme.exceptions.XmlReadingException;

public class NewTagTagHandler extends TagHandler {

	public String getTagName() {
		return "newtag";
	}
	
	public void startTag(Attributes attributes) {
		int handlerIndex = attributes.getIndex("handler");
		if(handlerIndex < 0)
			throw new XmlReadingException("Missing handler attribute on newtag tag");
		try {
			Class handler = Class.forName(attributes.getValue(handlerIndex));
			Object instance = handler.newInstance();
			if(!(instance instanceof TagHandler))
				throw new IllegalArgumentException("Given handler does not extends TagHandler");
			owner.addTagHandler(handler);
		} catch (Exception e) {
			throw new XmlReadingException("Exception while adding new tag: " 
					+ e.getMessage());
		}
	}
	
	public void endTag() {}
	
}
